package cn.tmpan.boot.service.impl;

import cn.tmpan.boot.common.ServerResponse;
import cn.tmpan.boot.dao.TestTableMapper;
import cn.tmpan.boot.model.TestTable;
import cn.tmpan.boot.service.ITestTableService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("iTestTableService")
@Slf4j

public class TestTableServiceImpl implements ITestTableService {

    @Autowired
    private TestTableMapper testTableMapper;


    @Override
    public ServerResponse<TestTable> detail(Integer id) {

        //查询用户信息 主键
        TestTable testTable = testTableMapper.selectByPrimaryKey(id);
        // 用户是否为空

        if (testTable == null) {
            return ServerResponse.createByErrorMessage("该用户不存在！");

        }
        return ServerResponse.createBySuccess("查询成功", testTable);
    }


    @Override
    public ServerResponse list(Integer pageSize, Integer pageCount, String orderBy) {

        PageHelper.startPage(pageSize, pageCount);

        //排序处理
        if (StringUtils.hasText(orderBy)) {

            String[] orderByArray = orderBy.split("_");
            PageHelper.orderBy(orderByArray[0] + " " + orderByArray[1]);


        }
        List<TestTable> testTables = testTableMapper.selectAll();
        PageInfo pageInfo = new PageInfo(testTables);
        return ServerResponse.createBySuccess("用户列表", pageInfo);
    }
}
